package com.first.nell.info

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.first.nell.R
import com.first.nell.components.dialog.PopListItem
import com.first.nell.components.form.*
import com.first.nell.extend.addFirstNellNormalFirstNellStyle
import com.first.nell.extend.dipFirstNell2FirstNellPx
import com.first.nell.extend.getFirstNellScreenFirstNellWidth

class InfoPageStepAdapter(private val dataList: List<PopListItem>) :
    RecyclerView.Adapter<InfoPageStepAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var textFirstNellAdpterFirstNellInfoFirstNellDot: TextView =
            view.findViewById(R.id.textAdpterInfoDot)
        var vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellLeft: View =
            view.findViewById(R.id.vAdpterInfoLineLeft)
        var vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellRight: View =
            view.findViewById(R.id.vAdpterInfoLineRight)
        var textFirstNellAdpterFirstNellInfoFirstNellTitle: TextView =
            view.findViewById(R.id.textAdpterInfoTitle)
        var imageFirstNellAdpterFirstNellInfoFirstNellFinishFirstNellDot: View =
            view.findViewById(R.id.imageAdpterInfoFinishDot)
        var imageFirstNellAdpterFirstNellInfoFirstNellCenterFirstNellDot: ImageView =
            view.findViewById(R.id.imageAdpterInfoCenterDot)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_first_nell_info_step, parent, false)
        //设置item宽度
        val horMargin = view.dipFirstNell2FirstNellPx(32F)
        val screenWidth = view.context.getFirstNellScreenFirstNellWidth() - horMargin
        var itemWidth = screenWidth
        if (dataList.isNotEmpty()) {
            itemWidth = screenWidth / dataList.size
        }
        view.layoutParams.width = itemWidth

        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data: PopListItem = dataList[position]
        //行线控制
        if (position == 0) {
            holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellLeft.visibility =
                View.INVISIBLE

        } else if (position == dataList.size - 1) {
            holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellRight.visibility =
                View.INVISIBLE

        } else {
            holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellLeft.visibility =
                View.VISIBLE
            holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellRight.visibility =
                View.VISIBLE
        }

        //线圈控制
        if (data.isFinished) {
            holder.imageFirstNellAdpterFirstNellInfoFirstNellCenterFirstNellDot.setImageResource(
                R.drawable.first_nell_info_step_done
            )

            holder.textFirstNellAdpterFirstNellInfoFirstNellDot.text = ""

            holder.imageFirstNellAdpterFirstNellInfoFirstNellFinishFirstNellDot.setBackgroundResource(
                R.drawable.first_nell_info_step_dot_selected_big
            )
            holder.textFirstNellAdpterFirstNellInfoFirstNellDot.setBackgroundResource(
                R.drawable.first_nell_info_step_done
            )
            holder.textFirstNellAdpterFirstNellInfoFirstNellDot.setTextColor(
                ContextCompat.getColor(
                    holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellRight.context,
                    R.color.white
                )
            )
            holder.textFirstNellAdpterFirstNellInfoFirstNellTitle.setTextColor(
                ContextCompat.getColor(
                    holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellRight.context,
                    R.color.black_7020
                )
            )
            holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellLeft.setBackgroundColor(
                ContextCompat.getColor(
                    holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellLeft.context,
                    R.color.main_blue
                )
            )
            holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellRight.setBackgroundColor(
                ContextCompat.getColor(
                    holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellRight.context,
                    R.color.main_blue
                )
            )

            holder.textFirstNellAdpterFirstNellInfoFirstNellTitle.addFirstNellNormalFirstNellStyle()

        } else {
            if (data.isChecked) {
                holder.imageFirstNellAdpterFirstNellInfoFirstNellCenterFirstNellDot.setImageResource(
                    R.drawable.first_nell_info_step_done
                )

                holder.imageFirstNellAdpterFirstNellInfoFirstNellFinishFirstNellDot.setBackgroundResource(
                    R.drawable.first_nell_info_step_dot_selected_big
                )
                holder.textFirstNellAdpterFirstNellInfoFirstNellDot.setBackgroundResource(
                    R.drawable.first_nell_info_step_dot_waiting
                )
                holder.textFirstNellAdpterFirstNellInfoFirstNellDot.setTextColor(
                    ContextCompat.getColor(
                        holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellRight.context,
                        R.color.white
                    )
                )
                holder.textFirstNellAdpterFirstNellInfoFirstNellTitle.setTextColor(
                    ContextCompat.getColor(
                        holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellRight.context,
                        R.color.black_7020
                    )
                )
                holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellLeft.setBackgroundColor(
                    ContextCompat.getColor(
                        holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellLeft.context,
                        R.color.main_blue
                    )
                )
                holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellRight.setBackgroundColor(
                    ContextCompat.getColor(
                        holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellRight.context,
                        R.color.black_7019
                    )
                )
                //更新显示
                holder.textFirstNellAdpterFirstNellInfoFirstNellDot.text = (position + 1).toString()
                holder.textFirstNellAdpterFirstNellInfoFirstNellTitle.addFirstNellNormalFirstNellStyle()

            } else {
                holder.imageFirstNellAdpterFirstNellInfoFirstNellCenterFirstNellDot.setImageResource(
                    R.drawable.first_nell_info_step_doing
                )

                holder.imageFirstNellAdpterFirstNellInfoFirstNellFinishFirstNellDot.setBackgroundResource(
                    R.drawable.first_nell_info_step_dot_unselect_big
                )
                holder.textFirstNellAdpterFirstNellInfoFirstNellDot.setBackgroundResource(
                    R.drawable.first_nell_info_step_dot_unselect_big
                )
                holder.textFirstNellAdpterFirstNellInfoFirstNellDot.setTextColor(
                    ContextCompat.getColor(
                        holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellRight.context,
                        R.color.black_7019
                    )
                )
                holder.textFirstNellAdpterFirstNellInfoFirstNellTitle.setTextColor(
                    ContextCompat.getColor(
                        holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellRight.context,
                        R.color.black_7019
                    )
                )
                holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellLeft.setBackgroundColor(
                    ContextCompat.getColor(
                        holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellLeft.context,
                        R.color.black_7019
                    )
                )
                holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellRight.setBackgroundColor(
                    ContextCompat.getColor(
                        holder.vFirstNellAdpterFirstNellInfoFirstNellLineFirstNellRight.context,
                        R.color.black_7019
                    )
                )
                //更新显示
                holder.textFirstNellAdpterFirstNellInfoFirstNellDot.text = (position + 1).toString()

                holder.textFirstNellAdpterFirstNellInfoFirstNellTitle.addFirstNellNormalFirstNellStyle()
            }


        }

        holder.textFirstNellAdpterFirstNellInfoFirstNellTitle.text = data.display
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}